package cn.rongdao.jrkabao.ui.setting;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import butterknife.BindView;
import butterknife.OnClick;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.setting.AboutUsBean;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jiangyongxing on 2017/5/18.
 * 描述：
 */

public class AboutUsActivity extends BaseAutoFinishActivity {

    @BindView(R.id.aau_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.aau_tv_version)
    TextView tvVersion;
    @BindView(R.id.aau_tv_weixin)
    TextView tvWeiXin;
    @BindView(R.id.aau_iv_weixin)
    ImageView ivWeiXin;
    @BindView(R.id.aau_rl_weixin)
    RelativeLayout rlWeiXin;
    @BindView(R.id.aau_tv_tip)
    TextView tvTip;
    private ClipboardManager clipboardManager;
    private String weiXin = "kamao99";
    private String weiXinContent = "卡猫微信号";

    public static void start(Context context) {
        FRStartActivity.start(context, AboutUsActivity.class);
    }

    @Override
    protected void initView() {
        clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        initTitleBar();
        initData();
        tvVersion.setText(FRCollection.appendAsString("V", BuildConfig.VERSION_NAME, ""));
        tvTip.setText(FRCollection.appendAsString("信用卡取现 ", CommonUtil.getAppNameByApplicationId(), ""));
    }

    private void initData() {
//        AboutUsModel.getInstance().execute(new FRBaseObserver<List<AboutUsBean>>(this, "请稍候...") {
//            @Override
//            protected void onBaseNext(List<AboutUsBean> data) {
//                if (null != data && data.size() > 0) {
//                    setWeiXinContent(data.get(0));
//                } else {
//                    setWeiXinContent(null);
//                }
//            }
//
//            @Override
//            protected void onBaseError(Throwable t) {
//                super.onBaseError(t);
//                setWeiXinContent(null);
//            }
//        });
    }

    private void setWeiXinContent(AboutUsBean data) {
        rlWeiXin.setVisibility(View.VISIBLE);
        if (null != data) {
            weiXin = data.getLogo_account();
            weiXinContent = data.getLogo_name();
            FRImageLoad.loadImage(FRString.valueOf(data.getLogo_url()), ivWeiXin);
        } else {
            ivWeiXin.setImageResource(R.drawable.icon_weixin);
        }
        setWeiXinStyle(weiXinContent, weiXin);
    }

    private void setWeiXinStyle(String name, String account) {
        String content = FRCollection.appendAsString(name, "\n", account, "");
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new AbsoluteSizeSpan(12, true), name.length(), content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvWeiXin.setText(ss);
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("关于我们");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setContentToClipBoard(String text) {
        clipboardManager.setPrimaryClip(ClipData.newPlainText(text, text));
        FRToast.showToastSafe("复制成功");
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_about_us;
    }

    @OnClick(R.id.aau_tv_copy)
    public void onViewClicked() {
        setContentToClipBoard(weiXin);
    }
}
